package com.example.fw;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;
import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

	public class ContactHelper extends WebDriverHelperBase {
		
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public ContactHelper createContact(ContactData contact) {
		initContactCreation();	
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		initContactModification(index);		
		fillContactForm(contact, MODIFICATION);
		updateContact();
		returnToHomePage();
		manager.getModel().removeContact(index).addContact(contact);
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		initContactModification(index);
		deletionContact();
		returnToHomePage();
		manager.getModel().removeContact(index);
		return this;
	}
	
//-----------------------------------------------------------------------

	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
	 
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			ContactData contact = new ContactData();
			String title = checkbox.getAttribute("title");
			String firstlastname = title.substring("Select (".length(), title.length() - ")".length());
			String[] titleArr = firstlastname.split(" ");
			contact.setFirstname(titleArr[0]);
			contact.setLastname(titleArr[1]);
			contacts.add(contact);
		}
		return contacts;
	}
	
	public ContactHelper deletionContact() {
		click(By.xpath("(//input[@name='update'])[2]"));
		return this;
	}
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		if (formType == CREATION) {
		//	selectByText(By.name("new_group"), contact.group);	
		} else {
		//	if (driver.findElement(By.name("new_group")) == null) {
		//	if driver.findElement(By.name("Group")).size() == 0) {
		//	if (!hasGroupSelector) {
		//		throw new Error("Group selector does not exist in contact modification form");
		//	}
		}
		return this;
	}
		
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}
	
	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactModification(int index) {
		manager.navigateTo().mainPage();
		selectContactByIndex(index);
		if (index == 0) { 
			click(By.xpath("//img[@alt='Edit']"));
		}
		else {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
		}
		return this;
	}
	
	private void selectContactByIndex(int index) {
		click(By.xpath("//table[@id='maintable']/tbody/tr[" + (index+2) + "]/td/input"));
	}
	
	public ContactHelper updateContact() {
		click(By.xpath("//input[@name='update']"));
		return this;
	}
}
